package com.github.shuaidd.rest.editor;

import com.github.shuaidd.service.editor.ActivitiFormService;
import lombok.AllArgsConstructor;
import org.activiti.app.model.editor.FormSaveRepresentation;
import org.activiti.app.model.editor.form.FormRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("app/rest/form-models")
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class FormResource {

    private final ActivitiFormService formService;

    @RequestMapping(value = "/{formId}", method = RequestMethod.GET, produces = "application/json")
    public FormRepresentation getForm(@PathVariable String formId) {
        return formService.getForm(formId);
    }

    @RequestMapping(value = "/values", method = RequestMethod.GET, produces = "application/json")
    public List<FormRepresentation> getForms(HttpServletRequest request) {
        String[] formIds = request.getParameterValues("formId");
        return formService.getForms(formIds);
    }

    @RequestMapping(value = "/{formId}/history/{formHistoryId}", method = RequestMethod.GET, produces = "application/json")
    public FormRepresentation getFormHistory(@PathVariable String formId, @PathVariable String formHistoryId) {
        return formService.getFormHistory(formId, formHistoryId);
    }

    @RequestMapping(value = "/{formId}", method = RequestMethod.PUT, produces = "application/json")
    public FormRepresentation saveForm(@PathVariable String formId, @RequestBody FormSaveRepresentation saveRepresentation) {
        return formService.saveForm(formId, saveRepresentation);
    }
}
